var utils = {
	isOverSizeFile: function(doc, sizeByte) {
		return File(doc.toString()).length > sizeByte;
	},
	zipFile: function(fileDoc, baseFolderName, fileName, quality, sizeByte) {
		var doc = app.activeDocument;
		var folderPath = baseFolderName;
		var folder = Folder(folderPath);
		if (!folder.exists) {
			folder.create();
		}

		if (utils.isOverSizeFile(fileDoc, sizeByte)) {
			var newDoc = File(folderPath +'/' + fileName);
			var saveOptions = new JPEGSaveOptions();
			saveOptions.quality = quality
			doc.saveAs(newDoc, saveOptions, true);

			if (utils.isOverSizeFile(newDoc, sizeByte) && quality > 1) {
			utils.zipFile(fileDoc, baseFolderName, fileName, quality - 1, sizeByte)
			}
		}
	},
	resolveFile: function(fileDoc, baseFolderName, fileName, sizeByte) {
		app.open(fileDoc);
		utils.zipFile(fileDoc, baseFolderName, fileName, 12, sizeByte);
		app.activeDocument.close();
	},
	resolveFolder: function(folderObj, baseName, sizeByte) {
		var folderList = folderObj.getFiles();
		var fileIndex = 1;
		for (var i = 0; i < folderList.length; i++) {
			var folderItem = folderList[i];
			if (folderItem instanceof File) {
				var extendName = folderItem.name.substr(-4);
				if (extendName && extendName.toLowerCase() === '.jpg')
					utils.resolveFile(folderItem, baseName, (fileIndex ++) + extendName, sizeByte);
			} else {
				utils.resolveFolder(folderItem, baseName + folderItem.name + '/', sizeByte);
			}
		}
	}
}

function TransClass(from, dist) {
	this.from = from;
	this.dist = dist;
	this.zip = function(sizeByte) {
		utils.resolveFolder(this.from,  this.dist.toString() + '/', sizeByte);
	}
}

var dialog=new Window (
	"dialog {\
		text: 'Setting',\
		size: [500, 250],\
		alignChildren: 'left',\
		fromPathSetting: Group {\
			from: StaticText{size:[60,16], text: 'From:'},\
			path: Button{size:[260,30],properties: {name: 'fromFolder'}, text:'Choose folder...'},\
		}\
		distPathSetting: Group {\
			dist: StaticText{size:[60,16],text: 'To:', paddings: 10},\
			path: Button{size:[260,30],properties: {name: 'toFolder'}, text:'Choose folder...'},\
		}\
		sizeSetting: Group{\
			prefix: StaticText{size:[60,16], text: 'Size Limit:'},\
			limitSize: EditText{size:[100,25], text:'3'},\
			limitType: DropDownList{size:[80,25], helpTip: 'select one'},\
		}\
		footer: Button{text: 'start', alignment: 'left'},\
	}\
");

var fromFolder, distFolder;
dialog.sizeSetting.limitType.add('item', 'mb');
dialog.sizeSetting.limitType.add('item', 'kb');
dialog.sizeSetting.limitType.selection = 0

dialog.fromPathSetting.path.onClick = function() {
	fromFolder = Folder.selectDialog ("please select from folder");
	dialog.fromPathSetting.path.text = decodeURI(fromFolder.toString());
}
dialog.distPathSetting.path.onClick = function() {
	distFolder = Folder.selectDialog ("please select dist folder");
	dialog.distPathSetting.path.text = decodeURI(distFolder.toString());
}
dialog.footer.onClick = function() {
	if (!fromFolder || !distFolder) {
		alert('fromFolder & distFolder can not be empty');
		return;
	}
	dialog.footer.visible = false
	var limitType = dialog.sizeSetting.limitType.selection;
	var limitSize = dialog.sizeSetting.limitSize.text;
	dialog.close()
	new TransClass(fromFolder, distFolder).zip(limitType.toString() === 'kb' ? limitSize * 1024 : limitSize * 1048576);
}
dialog.show()